package com.ruoyi.system.client.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.client.ServerChanIm;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ServerChanImImpl implements ServerChanIm {

    private static final String SEND_KEY = "sctp12027t91uusfncrcmjsqzx6aqjzn";

    public String scSend(String title, String content) {
        return scSend(title, content, SEND_KEY);
    }

    public String scSend(String title, String content, String tag) {
        return scSend(title, content, tag, SEND_KEY);
    }

    public String scSend(String title, String content, String tag, String sendKey) {
        try {
            String path;
            // 判断 sendkey 是否以 "sctp" 开头，并提取数字部分拼接 URL
            if (sendKey.startsWith("sctp")) {
                Pattern pattern = Pattern.compile("sctp(\\d+)t");
                Matcher matcher = pattern.matcher(sendKey);
                if (matcher.find()) {
                    String num = matcher.group(1);
                    path = "https://" + num + ".push.ft07.com/send/" + sendKey + ".send";
                } else {
                    throw new IllegalArgumentException("Invalid sendkey format for sctp");
                }
            } else {
                path = "https://sctapi.ftqq.com/" + sendKey + ".send";
            }

            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            JSONObject requestBodyJson = new JSONObject();
            requestBodyJson.put("text", title);
            requestBodyJson.put("desp", content);
            requestBodyJson.put("tags", tag);
            String requestBody = requestBodyJson.toString();
            // 写入请求体
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = requestBody.getBytes("utf-8");
                os.write(input, 0, input.length);
                os.flush();
            }

            int responseCode = connection.getResponseCode();
            BufferedReader var9 = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder var11 = new StringBuilder();

            String var10;
            while ((var10 = var9.readLine()) != null) {
                var11.append(var10);
            }

            var9.close();
            return var11.toString();
        } catch (Exception var12) {
            var12.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
        ServerChanImImpl serverChanIm = new ServerChanImImpl();
        String result = serverChanIm.scSend("测试标题", "测试内容", "CS");
    }
}
